#include "comm_monitor.h"
#include "function_interface.h"
#include "types.h"
#include <stdio.h>
#include "rated_limit.h"
#include "debug.h"
#include "syslog.h"
#include "logInterface.h"


static char *strCommDevice[] =
{
#define X_MACRO(a) #a,
    COMM_DEVICE_TABLE
#undef X_MACRO    
};


int CommMonitorCallback_InitToOnline(commDevice_t dev)
{
    if(dev >= COMM_DEVICE_QTY)
    {
        PRINT_WARN();
        return -1;
    }

    RatedLimit_SetPercentage((ratedLimitOwner_t)(dev + 16), 1.0F);
    syslog(LOG_INFO, "%s: %s Init -> Online", __func__, strCommDevice[dev]);
    PRINT_INFO("%s communication state: Init -> Online", strCommDevice[dev]);
    // logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_COMM, dev + 100, COM_DATA_TYPE_NONE, NULL);
    // logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, dev, COM_DATA_TYPE_NONE, NULL);

    return 0;
}


int CommMonitorCallback_OfflineToOnline(commDevice_t dev)
{
    if(dev >= COMM_DEVICE_QTY)
    {
        PRINT_WARN();
        return -1;
    }

    RatedLimit_SetPercentage((ratedLimitOwner_t)(dev + 16), 1.0F);
    syslog(LOG_INFO, "%s: %s Offline -> Online", __func__, strCommDevice[dev]);
    PRINT_INFO("%s communication state: Offline -> Online", strCommDevice[dev]);
    logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_COMM, dev + 120, COM_DATA_TYPE_NONE, NULL);
    logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, dev + 20, COM_DATA_TYPE_NONE, NULL);

    return 0;
}


int CommMonitorCallback_InitToOffline(commDevice_t dev)
{
    if(dev >= COMM_DEVICE_QTY)
    {
        PRINT_WARN();
        return -1;
    }

    RatedLimit_SetPercentage((ratedLimitOwner_t)(dev + 16), 0.0F);
    syslog(LOG_INFO, "%s: %s Init -> Offline", __func__, strCommDevice[dev]);
    PRINT_INFO("%s communication state: Init -> Offline", strCommDevice[dev]);
    // logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_COMM, dev + 140, COM_DATA_TYPE_NONE, NULL);
    // logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, dev + 40, COM_DATA_TYPE_NONE, NULL);

    return 0;
}


int CommMonitorCallback_OnlineToOffline(commDevice_t dev)
{
    if(dev >= COMM_DEVICE_QTY)
    {
        PRINT_WARN();
        return -1;
    }

    RatedLimit_SetPercentage((ratedLimitOwner_t)(dev + 16), 0.0F);
    syslog(LOG_INFO, "%s: %s Online -> Offline", __func__, strCommDevice[dev]);
    PRINT_INFO("%s communication state: Online -> Offline", strCommDevice[dev]);
    logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_COMM, dev + 160, COM_DATA_TYPE_NONE, NULL);
    logInterfaceLogAdd(LOG_CLASS_SPECIAL, LOG_MAINTYPE_RATED_LIMIT, dev + 60, COM_DATA_TYPE_NONE, NULL);

    return 0;
}

